package BF;

import redis.clients.jedis.Jedis;

import java.util.ArrayList;
import java.util.BitSet;
import java.util.List;

public class BF {


    public static void main(String[] args) {
        //连接本地的 Redis 服务
//        Jedis jedis = new Jedis("127.0.0.1",6378);
//        jedis.auth("123456");
//        System.out.println("连接成功");
//        //设置 redis 字符串数据
//
//        for (int i=0;i<10000;i++){
////            jedis.bitfield(i,"1");
//            jedis.setbit("java",i, String.valueOf(1));
//        }
////        jedis.("runoobkey", "www.runoob.com");
//
////        jedis.
//        // 获取存储的数据并输出
//        System.out.println("redis 存储的字符串为: "+ jedis.getbit("java",1));
        java();
    }


    public static int MAGIC_NUM = 10;


    public static List<String> urls = new ArrayList<>();
    static {
        urls.add("www.baidu.com");
        urls.add("www.google.com");
        urls.add("www.weidian.com");
        urls.add("www.baidu.com");
        urls.add("www.google.com");
    }

    public static void java() {
        int bucketSize = 100000;
        BitSet bitSet = new BitSet(bucketSize);

        for(String url : urls) {
            int h = Math.abs(url.hashCode());
            int index = getIndex(h,bucketSize);
            int[] tmpQ = new int[MAGIC_NUM];
            tmpQ[0] = index;
            for(int i = 1; i < MAGIC_NUM; i++) {
                h = rehash(h);
                index = getIndex(h,bucketSize);
                tmpQ[i] = index;
            }

            boolean isExist = true;
            for(int ind : tmpQ) {
                if(bitSet.get(ind)) {
                    continue;
                }
                if(isExist) {
                    isExist = false;
                }
                bitSet.set(ind,true);
            }
            if(isExist) {
                System.out.println("url is " + url);
            }
        }
    }

    private static int getIndex(int h,int m) {
        return (h/m)%m;
    }

    private static int rehash(int h) {
        return Math.abs(h^(h >>> 16));
    }


}
